<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Constants\QmGame;
use App\Controller\InsideService\CoinInside;
use App\Middleware\AdminMiddleware;
use App\Service\ExcelService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: '/admin/Coin'), Middleware(AdminMiddleware::class)]
class CoinController extends BaseController {

    /**
     * 获取列表
     * @return array
     * @throws \Exception
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $userDataArr = $this->uInfo();
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $game_hash = $this->request->query('game_hash', '');
        $openid = $this->request->query('openid', '');
        $type = $this->request->query('type', 0);
        $date = $this->request->query('date', '');
        $page = $this->request->query('page', 1);

        if (empty($game_hash)) {
            $game_hash = $userDataArr['user_data']['bind_games'];
        }

        return (new CoinInside())->setCode($userDataArr['connection_code'])->index([
            'page'      => $page,
            'openid'    => $openid,
            'type'      => $type,
            'date'      => $date,
            'size'      => $limit,
            'game_hash' => $game_hash
        ]);
    }

    /**
     * 获取列表
     * @return array|ResponseInterface
     * @throws \Exception
     */
    #[GetMapping(path: 'export')]
    public function export(): array|ResponseInterface {
        set_time_limit(0);
        ini_set('memory_limit', '5048M');
        $userDataArr = $this->uInfo();
        $game_hash = $this->request->query('game_hash', '');
        $openid = $this->request->query('openid', '');
        $type = $this->request->query('type', 0);
        $date = $this->request->query('date', '');

        if (empty($game_hash)) {
            $game_hash = $userDataArr['user_data']['bind_games'];
        }

        $data = (new CoinInside())->setCode($userDataArr['connection_code'])->index([
            'page'      => 0, //不分页
            'openid'    => $openid,
            'type'      => $type,
            'date'      => $date,
            'game_hash' => $game_hash
        ]);

        if ($data['data']['count'] <= 0) {
            return $this->buildFailed(ErrorCode::INVALID, '1'); //返回标识 前端返回提示
        }

        if ($data['data']['count'] > 1000) {
            return $this->buildFailed(ErrorCode::INVALID, '11'); //返回标识 前端返回提示
        }

        $fileName = '点券数据-' . date('YmdHis');
        $title = [
            'ID',
            '所属游戏',
            'openid',
            '角色ID',
            '使用状态',
            '点券数量',
            '变更前点券余额',
            '记录时间',
            '来源类型',
            '来源ID',
            '来源数量',
            '来源描述',
        ];
        //表体数据
        $list = $data['data']['list'];
        $data = [];
        $typeArr = [
            1 => '消费',
            2 => '获得',
            3 => '订单充值',
        ];
        foreach ($list as $value) {
            //获取游戏名称
            $data[] = [
                $value['id'],
                QmGame::GAME_LIST[$value['game_hash']],
                $value['openid'],
                $value['role_id'],
                $typeArr[$value['type']],
                $value['coin'],
                $value['coin_balance'],
                $value['created_at'],
                $value['source_type'],
                $value['source_id'],
                $value['source_num'],
                $value['source_desc'],
            ];
        }
        $exportService = new ExcelService();

        //保存临时文件在从浏览器自动下载到本地
        return $exportService->setHeader($title)->addData($data)->saveToBrowserByTmp($fileName);
    }
}